package com.hzdesk.media.koreabatchmatvector.utils;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.util.Random;

/**
 * Created by Administrator on 2018/5/18.
 */

public class MacUtil {
    /**
     * Gets the mac address.
     *
     * @return the mac address
     */
    public static String getMacAddress(Context context) {
        String macAddress = "";
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Log.d("MacAddress", "6.0以下");
            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            macAddress = wm.getConnectionInfo().getMacAddress();

        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            macAddress = getMac(context);
          //  Log.d("MacAddress", "6.0以上：" + macAddress);

        }
        if (macAddress.equals("") || macAddress == null || macAddress.equals("02:00:00:00:00:00")) {
            String macpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mac.txt";
            if (FileUtil.isFileExist(macpath)) {
                macAddress = FileUtil.readFile(new File(macpath));
            } else {
                macAddress = getRandomMac();
                FileUtil.write(macpath, macAddress);
            }
            Log.d(">>>>>>", macAddress);
        }

        String[] arr = macAddress.split(":");
        String mac = "";
        for (int i1 = 0; i1 < arr.length; i1++) {
            mac = mac + arr[i1];
        }
        Log.d("MacAddress", "mac 地址 ：" + mac);
        return mac;
    }

    private static String getRandomMac() {
        String Mac = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 2; j++) {
                Mac = Mac + random.nextInt(9);
            }
            if (i != 5) {
                Mac = Mac + ":";
            }
        }
        return Mac;
    }

    private static String getMac(Context context) {
        String str = "";
        String macSerial = "";
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (Exception ex) {
            Log.e("----->" + "NetInfoManager", "getMacAddress:" + ex.toString());
        }
        if (macSerial == null || "".equals(macSerial)) {
            try {
                return loadFileAsString("/sys/class/net/eth0/address").toUpperCase().substring(0, 17);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("----->" + "NetInfoManager", "getMacAddress:" + e.toString());
            }

        }
        return macSerial;
    }

    private static String loadFileAsString(String fileName) throws Exception {
        FileReader reader = new FileReader(fileName);
        String text = loadReaderAsString(reader);
        reader.close();
        return text;
    }

    private static String loadReaderAsString(Reader reader) throws Exception {
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[4096];
        int readLength = reader.read(buffer);
        while (readLength >= 0) {
            builder.append(buffer, 0, readLength);
            readLength = reader.read(buffer);
        }
        return builder.toString();
    }

}
